<?php
/**
 * Created by JetBrains PhpStorm.
 * File: CategoryProduct.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-23
 * Time: 下午6:45
 */

class PcategoryAction extends CommonAction{

	//指向分类列表
	function index(){

			$pcategory = D('Pcategory');
			import("ORG.Util.page");
			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}
					$count = $pcategory->where($datas)->count('id');
					if($count<1){
						$this->error('没有该分类数据');
					}elseif($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$list = $pcategory->where($datas)->order('id desc')->select();
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
				}
			}else{
				$count = $pcategory->count();
				if($count<1){
					$this->assign('isshow', 'no');
					$this->assign('nobody', 'no');
				}elseif($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$page = new page($count, C('PAGENUM'));
				$show = $page->fpage($pagedata);
				$list = $pcategory->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
				foreach($list as $key=>$value){
					$list[$key]['count'] = count($value['products']);
				}
			}
			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();

	}

	//指向添加分类页面
	function add(){

			$this->display('add');


	}

	//添加分类
	function insert(){

			$pcategory = D('Pcategory');
			if($list = $pcategory->create()){
				$list['path'] = time();
				if(mkdir('./Public/pcategory/'.$list['path'], 0755)){
					if($pcategory->add($list)){
						$this->success('添加产品分类成功', __APP__.'/Pcategory/index');
					}else{
						$this->error('添加产品分类失败');
					}
				}else{
					$this->error('创建存放产品分类目录失败');
				}
			}else{
				$this->error($pcategory->getError());
			}

	}

	//删除单元
	function del(){

			if(isset($_POST['del']) && !empty($_POST['del'])){
				$pcategory = D('Pcategory');

				foreach($_POST['del'] as $key=>$value){
					$list = $pcategory->relation(true)->where('id='.$value)->select();

					foreach($list as $k=>$v){

						if(!empty($v['products'])){
							$this->error('[ '.$v['name'].' ]分类下还有产品, 请先清空');
						}else{
							$num = $pcategory->relation(true)->delete($value);
							rmdir('./Public/pcategory/'.$v['path']);
						}
					}
					if($num>0){
						$this->success('删除分类成功', __APP__.'/Pcategory/index/');
					}else{
						$this->error('删除分类失败');
					}
				}

			}else{
				$this->error('异常操作');
			}
	}

	//指向修改分类
	function edit(){

			if($_GET['id'] && !empty($_GET['id'])){
				$pcategory = M('Pcategory');
				$list = $pcategory->find($_GET['id']);
				$this->assign('list', $list);
				$this->display();
			}else{
				$this->error('异常操作');
			}

	}

	//修改分类
	function update(){

				if(isset($_POST['id']) && !empty($_POST['id'])){
					$pcategory = D('Pcategory');
					if($pcategory->create()){
						if($pcategory->save()){
							$this->success('修改产品分类成功', __APP__.'/Pcategory/index');
						}else{
							$this->error('修改产品分类失败');
						}
					}else{
						$this->error($pcategory->getError());
					}
				}else{
					$this->error('异常操作');
				}
	}


	function select(){

		$category = D('Category');
		import("ORG.Util.page");

		if(isset($_POST['search'])){
			if(empty($_POST['key'])){
				$this->error('不能为空');
			}else{
				$datas = array();
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
				}

				if(isset($_SESSION['mid']) && !empty($_SESSION['mid'])){
					if(isset($_SESSION['cid']) && !empty($_SESSION['cid'])){

						$list = $category->where($datas)->order('id desc')->select();
						$count = $category->where($datas)->count('id');
						if($count<1){
							//	$this->assign('isshow', 'no');
							$this->error('没有该分类数据');
						}elseif($count<C('PAGENUM')){
							$pagedata = array(1);
						}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
						}
						$page = new page($count, C('PAGENUM'));

						$show = $page->fpage($pagedata);
						$this->assign('isshow', 'yes');
						$this->assign('nobody', 'yes');

					}else{
						$this->error('异常操作');
					}
				}else{
					$this->error('异常操作');
				}


			}

		}else{

			if(isset($_GET['mid']) && !empty($_GET['mid'])){
				if(isset($_GET['cid']) && !empty($_GET['cid'])){
					$_SESSION['mid'] = $_GET['mid'];
					$_SESSION['cid'] = $_GET['cid'];
					$count = $category->count();
					if($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));
					$show = $page->fpage($pagedata);
					$list = $category->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
					//	dump($list);
					if($count==0){
						$this->assign('isshow', 'no');
						$this->assign('nobody', 'no');
					}else{
						$this->assign('isshow', 'yes');
						$this->assign('nobody', 'yes');
					}
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}


		}

		$this->assign('show', $show);
		$this->assign('list', $list);
		$this->display();

	}


	}